<?php
defined('_JEXEC') or die( 'Restricted access' );
jimport('joomla.application.component.model');
jimport('joomla.filesystem.file');
class DanhMucModelDetail extends JModel
{
	var $_id = null;
	var $_data = null;
	function __construct()
	{
		parent::__construct();
		$array = JRequest::getVar('cid', array(0), '', 'array');
		$edit	= JRequest::getVar('edit',true);
		if($edit)
			$this->setId((int)$array[0]);
	}
	function setId($id)
	{
		$this->_id		= $id;
		$this->_data	= null;
	}
	function &getData(){		
		if ($this->_loadData()){
			
		}else{
			$this->_initData();
		}
		return $this->_data;
	}
	function delete($cid = array())
	{
		global $option,$mainframe;
		$db =& JFactory::getDBO();
		$result = false;
		if(count($cid)>1)
		{
			foreach($cid as $key=>$value)
			{
				$query = 'select * from #__category where parent_id= '.$value;
				$db->setQuery($query);
				$row = $db->loadResult();
				if(count($row)){
					$mainframe->redirect('index.php?option='.$option,'Danh Mục Có Danh Mục Con. Không Thể Xóa');	
				}
				$query = 'select * from #__product where parent= '.$value;
				$db->setQuery($query);
				$row = $db->loadResult();
				if(count($row)){
					$mainframe->redirect('index.php?option='.$option,'Danh Mục Còn Sản Phẩm. Không Thể Xóa');	
				}							
				$query = "DELETE FROM #__category WHERE id =".$value;	
				$db->setQuery( $query );
				if (!$db->query())
				{
					$this->setError($this->_db->getErrorMsg());
					return false;
				}
			}
		}
		else
		{
			$query = 'select * from #__category where parent_id= '.$cid[0];
			$db->setQuery($query);
			$row = $db->loadResult();
			if(count($row)){
				$mainframe->redirect('index.php?option='.$option,'Danh Mục Có Danh Mục Con. Không Thể Xóa');
				return false;	
			}
			$query = 'select * from #__product where parent= '.$cid[0];
			$db->setQuery($query);
			$row = $db->loadResult();
			if(count($row)){
				$mainframe->redirect('index.php?option='.$option,'Danh Mục Còn Sản Phẩm. Không Thể Xóa');
				return false;	
			}
			$query = "DELETE FROM #__category WHERE id =".$cid[0];	
			$db->setQuery( $query );
			if (!$db->query())
			{
				$this->setError($this->_db->getErrorMsg());
				return false;
			}
		}
		return true;
		
	}
	function publish($cid = array(), $publish = 1)
	{
		$user 	=& JFactory::getUser();
		if (count( $cid ))
		{
			JArrayHelper::toInteger($cid);
			$cids = implode( ',', $cid );

			$query = 'UPDATE #__category'
				. ' SET published = '.(int) $publish
				. ' WHERE id IN ( '.$cids.' )';
			$this->_db->setQuery( $query );
			if (!$this->_db->query()) {
				$this->setError($this->_db->getErrorMsg());
				return false;
			}
		}
		return true;
	}
	function store($data)
	{
		$row =& $this->getTable('danhmuc');
		// Bind the form fields to the web link table
		if (!$row->bind($data)) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}
		if (!$row->id) {
			$where = 'parent_id = ' . (int) $row->parent_id ;
			$row->ordering = $row->getNextOrder( $where );
		}
		
		// Store the web link table to the database
		if (!$row->store()) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}
		return true;
	}
	function move($direction)
	{
		$row =& $this->getTable('danhmuc');
		if (!$row->load($this->_id)) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}
		if (!$row->move( $direction, ' parent_id = '.(int) $row->parent_id.' AND published >= 0 ' )) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}
		return true;
	}
	function saveorder($cid = array(), $order)
	{
		$row =& $this->getTable('danhmuc');
		$groupings = array();
		// update ordering values
		for( $i=0; $i < count($cid); $i++ )
		{
			$row->load( (int) $cid[$i] );
			// track categories
			$groupings[] = $row->parent_id;

			if ($row->ordering != $order[$i])
			{
				$row->ordering = $order[$i];
				if (!$row->store()) {
					$this->setError($this->_db->getErrorMsg());
					return false;
				}
			}
		}
		// execute updateOrder for each parent group
		$groupings = array_unique( $groupings );
		foreach ($groupings as $group){
			$row->reorder('parent_id = '.(int) $group);
		}
		return true;
	}
	function _loadData(){
		// Lets load the content if it doesn't already exist
		if (empty($this->_data)){
			$query = 'SELECT * FROM #__category'.
					' WHERE id = '.(int) $this->_id;
			$this->_db->setQuery($query);
			$this->_data = $this->_db->loadObject();
			return (boolean) $this->_data;
		}
		return true;
	}
	function _initData(){
		if (empty($this->_data)){
			$sp = new stdClass();
			$sp->id				= 0;
			$sp->parent_id				= null;
			$sp->ordering			= null;
			$sp->title			= null;
			$sp->published         = 1;
			$sp->created = null;		
			$this->_data = $sp;
			return (boolean) $this->_data;
		}
		return true;
	}
}